#!/bin/env python3
# -*- coding: utf-8 -*- (for python2.x)

def menu(l, title=None, p=None):
    while 1:
        i = 1
        if title is not None:
            print('\n ####', title)
        for item in l:
            print(' # %3d %s' % (i, item['name']))
            i += 1
        print(' #   0 return')

        if p is None:
            try:
                n = int(input('input item number:'))
            except Exception as e:
                print('get number failed: %s' % e)
                continue
        else:
            n = p

        #  print('get item number:%d' % n)
        if n == 0:
            print('quit\n')
            break
        elif n < i and n > 0:
            n -= 1
            #  print('enter function: %r' % l[n]['func'])
            print(' ####')
            func = l[n].get('func')
            if func is not None and func != 0:
                func_param = None
                if 'param' in l[n].keys():
                    func_param = l[n]['param']
                func(func_param)
            if p is not None:
                break
        else:
            print('number(%d) out of range(0~%d)' % (n, i - 1))
            p = None;
        n = None;

